﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YinfangWeb.BLL.Enum;
using YinfangWeb.BLL.Model;
using YinfangWeb.DAL;
using YinfangWeb.Model;

namespace YinfangWeb.BLL
{
    public class OrderBO
    {
        private readonly string HttpBaseUrl = ConfigurationManager.AppSettings["HttpFileBaseUrl"];
        private readonly OrderCartDAO _ocartDAO = new OrderCartDAO();

        //加入商品到购物车
        public IResult AddOrderCar(ObjUserAccountInfo ut, OrderCart orderCar)
        {
            int result = 0;
            var carPd = _ocartDAO.GetInfo(ut.UserDataID, orderCar.MerchantDataID,orderCar.ProductParamID);
            if (carPd == null)//购物车中没有该商品：添加
            {
                orderCar.RegUserID = orderCar.EditUserID = ut.UserDataID;
                orderCar.RegDateTime = orderCar.EditDateTime = DateTime.Now;
                result = _ocartDAO.Insert(orderCar);
            }
            else//购物车中已有该商品：数量累加
            {
                carPd.Number += orderCar.Number;
                result = _ocartDAO.Update(carPd);
            }
            if (result > 0) return new Result();
            else return new Result(ResultCode.Busy);
        }
        //获取用户在购物车的商品小卡片列表
        public IResult GetOrderCarList(int UserID)
        {
            
            var list = _ocartDAO.GetList(UserID);
            var result = list.Select(oc => new ObjOrderCartSmallCard
            {
                OrderCartID = oc.OrderCartID,
                FileName = $"{HttpBaseUrl}/product/{oc.ProductDataID}/5/{oc.FileName}",
                ProductName = oc.ProductName,
                PriceMarket = oc.PriceMarket.ToString("#0.00"),
                PriceSell = oc.PriceSell.ToString("#0.00"),
                Number = oc.Number
            }).ToList();
            return new DataResult<List<ObjOrderCartSmallCard>>(result);
        }
    }
}
